package com.uyayo.oms.system.controller;

import com.uyayo.framework.common.annotation.MethodName;
import com.uyayo.framework.excelhelper.service.BaseExportExcelService;
import com.uyayo.framework.message.model.Result;
import com.uyayo.oms.system.biz.ParamBiz;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.pojo.PageResult;
import com.uyayo.system.monitor.entity.SysEvent;
import com.uyayo.system.monitor.model.SysEventCondition;
import com.uyayo.system.monitor.service.EventsService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 
 * <p>
 * Title: SystemEventController
 * </p>
 * <p>
 * Description:系统事件监听的controller
 * </p>
 * <p>
 * Company: www.lelekeji.com
 * </p>
 * 
 * @author 乐乐科技.
 * @date 2015-4-13下午4:03:35
 * @version 1.0
 */
@Controller
@RequestMapping("/system/monitor/event")
public class SysEventMonitorController {

	@Autowired
	@Qualifier("baseExportExcelXSSFService")
	private BaseExportExcelService baseExportExcelService;

	@Autowired 
	private EventsService eventsService;
	
	@Autowired
	private ParamBiz paramBiz;
	
	@RequestMapping(value="index",method=RequestMethod.GET)
	@MethodName(value="系统管理->系统监控->系统事件监控")
	public String index() throws Exception {
		return paramBiz.getManagerTemplatesPrefix()+"system/monitor/event/index";
	}
	
	@ResponseBody
	@RequestMapping(value="grid",method=RequestMethod.GET)
	@MethodName(value="系统管理->系统监控->系统事件监控->查询列表",type=2)
	public  PageResult<SysEvent> event_grid(@RequestParam(value = "offset",required = false, defaultValue = "1") Integer offset, //第多少行之后开始
            @RequestParam(value = "limit", required = false, defaultValue = "10") Integer pageSize,String sort,String order,SysEventCondition sysEventCondition) throws Exception {
		PageBean<SysEvent> pageBean=new PageBean<>();
		try {
			pageBean = eventsService.selectEvent((offset / pageSize) + 1, pageSize,sort,order,sysEventCondition);
		} catch (Exception e) {
			e.printStackTrace();		
		}
		PageResult<SysEvent> pageResult = new PageResult<>();
		pageResult.setRows(pageBean.getList());
		pageResult.setTotal(pageBean.getTotalCount());
		return pageResult;
	}
	
	
	/**
	 * 查看
	 */
	@RequestMapping(value = "view", method = RequestMethod.GET)
	@MethodName(value="系统管理->监控管理->系统事件监控->查看",type=2)
	@RequiresPermissions("system:monitor:event:view")
	public ModelAndView show(ModelAndView modelAndView,String id) {
		SysEvent sysEvent = new SysEvent();
		try {
			sysEvent = eventsService.getEventById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelAndView.addObject("sysEvent",sysEvent);
		modelAndView.setViewName(paramBiz.getManagerTemplatesPrefix()+"system/monitor/event/view");
		return modelAndView;
	}
	
	/**
	 * 批量删除记录
	 */
	@ResponseBody
	@RequiresPermissions("system:monitor:event:delete")
	@RequestMapping(value="delete",method=RequestMethod.DELETE)
	@MethodName(value="系统管理->系统监控->系统事件监控->删除系统事件监控",type=2)
	public Result deleteEvent(String ids) {
		try {
			int deleteCount = eventsService.deleteEvent(ids);
			if(deleteCount<=0){
				return Result.errorAndMsg("删除失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.errorAndMsg("删除失败！");
		}
		return Result.successAndMsg("删除成功！");
	}
	
	@RequestMapping(value = "export", method = RequestMethod.GET)
	@RequiresPermissions(value = "system:monitor:event:export")
	@MethodName(value="系统管理->系统监控->SQL执行监控->导出Excel",type=2)
	public ModelAndView exportExcel(HSSFWorkbook workbook, HttpServletResponse response, SysEventCondition sysEventCondition) throws Exception {
		response.setContentType("octets/stream;charset=UTF-8");
		
		String fileName = new String("系统事件监控".getBytes("UTF-8"),"ISO8859-1");
	    response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
		    
	    String[] headers = {"事件编号 ","事件路径 ","事件详情","事件开始时间","事件结束时间"};
	    String[] fieldNames = {"code","requestUrl","description","startTime","endTime"};

	    List<SysEvent> eventsList;
	    
	    try {
	         
	    	eventsList = eventsService.selectEvent(sysEventCondition);
	         
	        OutputStream out = response.getOutputStream();
			this.baseExportExcelService.exportExcel(workbook,0,"系统操作事件监控表",headers,fieldNames, eventsList, "yyyy-MM-dd HH:mm:ss");
	        workbook.write(out);
	        out.close();

	    } catch (FileNotFoundException e) {

	         e.printStackTrace();

	    } catch (IOException e) {

	         e.printStackTrace();

	    }
		return null;
	}
	
	
}
